/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.category.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.bean.DataokeGetCategorySubDataBean;

import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class DetailRecycleAdapter extends RecyclerView.Adapter<WQViewHolder> {
    private int layoutId;
    private List<DataokeGetCategorySubDataBean> data;
    public Context context;
    private OnItemClickListner onItemClickListner;//单击事件
    private OnItemLongClickListner onItemLongClickListner;//长按单击事件
    private boolean clickFlag = true;//单击事件和长单击事件的屏蔽标识

    /**
     * @param context  //上下文
     * @param layoutId //布局id
     * @param data     //数据源
     */
    public DetailRecycleAdapter(Context context, int layoutId, List<DataokeGetCategorySubDataBean> data) {
        this.layoutId = layoutId;
        this.data = data;
        this.context = context;
    }


    @Override
    public WQViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        final WQViewHolder holder = new WQViewHolder(v, context);
        //单击事件回调
        v.setOnClickListener(v1 -> {
            if (onItemClickListner == null)
                return;
            if (clickFlag) {
                onItemClickListner.onItemClickListner(v1, holder.getLayoutPosition());
            }
            clickFlag = true;
        });
        //单击长按事件回调
        v.setOnLongClickListener(v12 -> {
            if (onItemLongClickListner == null)
                return false;
            onItemLongClickListner.onItemLongClickListner(v12, holder.getLayoutPosition());
            clickFlag = false;
            return false;
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(WQViewHolder holder, int position) {
        if (mCallBack != null)
            mCallBack.convert(holder, data.get(position), position);
    }

    @Override
    public int getItemCount() {
        int count = data.size();
        return count;
    }


    public void setOnItemClickListner(OnItemClickListner onItemClickListner) {
        this.onItemClickListner = onItemClickListner;
    }
    public interface OnItemClickListner {
        void onItemClickListner(View v, int position);
    }
    public void setOnItemLongClickListner(OnItemLongClickListner onItemLongClickListner) {
        this.onItemLongClickListner = onItemLongClickListner;
    }



    public interface OnItemLongClickListner {
        void onItemLongClickListner(View v, int position);
    }

    CallBack mCallBack;

    public void setCallBack(CallBack CallBack) {
        this.mCallBack = CallBack;
    }

    public interface CallBack {
        void convert(WQViewHolder holder, DataokeGetCategorySubDataBean bean, int position);
    }

}
